package com.ymttest.business.service;

import com.ymatou.iapi.notify.parameter.GetNotifyCountBean;
import com.ymatou.iapi.notify.parameter.SendNotifyBean;
import com.ymatou.iapi.notify.parameter.SendSpeedNotifyBean;
import com.ymatou.iapi.notify.service.GetNotifyCountCall;
import com.ymatou.iapi.notify.service.SendSpeedNotifyCall;
import com.ymatou.im.api.service.SendNotifyCall;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

public class NotifyCallService {
	public static int SHOP2CN_FROM_NOTIFY=19000300;
    /**
     * 初始化发送通知Bean
     * @param notifySenderUserId 通知大号
     * @param toUserId 接收方
     * @return
     */
    public static SendNotifyBean initSendNotify(int notifySenderUserId,int toUserId) throws ParseException {
        SendNotifyBean sendNotifyBean = new SendNotifyBean();
        sendNotifyBean.setNotifySenderUserId(notifySenderUserId);
        sendNotifyBean.setToUserId(toUserId);
        sendNotifyBean.setMessage("nihao，alabo");
        String date1 = YMTDateUtil.getBeforeOrNextDay(1,YMTDateUtil.YMDHMS);
        sendNotifyBean.setEndTime(date1);
        sendNotifyBean.setPushTitle("测试");
        sendNotifyBean.setIsComment(true);
        sendNotifyBean.setIsPushNotify(true);
        sendNotifyBean.setSkipSaveToDb(false);
        return sendNotifyBean;
    }

    /**
     * 发送通知
     * @param sendNotifyBean
     * @throws IOException
     */
    public static void SendNotify(SendNotifyBean sendNotifyBean) throws IOException {
        SendNotifyCall sendNotifyCall = new SendNotifyCall();
        sendNotifyCall.setData(sendNotifyBean);
        sendNotifyCall.callService();
    }

    /**
     * 获取通知未读数
     * @param userId 用户id
     * @param senderIdList 通知号列表
     * @return
     * @throws IOException
     */
    public static Integer GetNotifyCount(int userId, List<Integer> senderIdList) throws IOException {
        GetNotifyCountBean getNotifyCountBean  = new GetNotifyCountBean();
        getNotifyCountBean.setUserId(userId);
        getNotifyCountBean.setSenderIdList(senderIdList);
        GetNotifyCountCall getNotifyCountCall = new GetNotifyCountCall();
        getNotifyCountCall.setData(getNotifyCountBean);
        getNotifyCountCall.callService();
        String[] arr1 = getNotifyCountCall.getString("Result").split(":");
        String[] arr2 = arr1[1].split("}");
        return Integer.parseInt(arr2[0]);
    }

    /**
     * 初始化保存批量通知内容
     * @param notifySenderUserId
     * @return
     */
    public static SendSpeedNotifyBean initSendSpeedNotify(int notifySenderUserId){
        SendSpeedNotifyBean sendSpeedNotifyBean = new SendSpeedNotifyBean();
        sendSpeedNotifyBean.setNotifySenderUserId(notifySenderUserId);
        String date = YMTDateUtil.getBeforeOrNextDay(1,"dd/MM/yyyy HH:mm:ss");
        sendSpeedNotifyBean.setEndTime(date);
        sendSpeedNotifyBean.setIsPushNotify(true);
        sendSpeedNotifyBean.setMessage("测试批量通知");
        sendSpeedNotifyBean.setPushTitle("测试"+ TestDataManager.getRandomNum(5));
        return sendSpeedNotifyBean;
    }

    /**
     * 保存批量通知内容
     * @param sendSpeedNotifyBean
     * @return
     * @throws IOException
     */
    public static int SendSpeedNotify(SendSpeedNotifyBean sendSpeedNotifyBean) throws IOException {
        SendSpeedNotifyCall sendSpeedNotifyCall = new SendSpeedNotifyCall();
        sendSpeedNotifyCall.setData(sendSpeedNotifyBean);
        sendSpeedNotifyCall.callService();
        return sendSpeedNotifyCall.getJsonObject().get("Result").getAsJsonObject().get("NotifyId").getAsInt();
    }
}
